package com.jshx.sz.gdjt.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.control.TextEditView;
import com.jshx.sz.gdjt.db.DataBaseUtil;
/**
 * 备注Fragment
 * @author xiangchangan
 * @time 2013-08-22
 */
public class RemarkFrame extends Fragment 
{
	/**
	 * 添加备注按钮
	 */
	private ImageButton addRemark;
	
	/**
	 * 备注容器
	 */
	private LinearLayout remarks;
	
	/**
	 * 组件实例化工具
	 */
	private LayoutInflater layoutInflater;
	
	//
	private HashMap<String, String> data;
	
	//
	private ArrayList<View> viewList;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.remark_fragment, null);
		layoutInflater = LayoutInflater.from(this.getActivity());
		initView(view);
		return view;
	}

	/**
	 * 添加备注视图
	 */
	private View addRemarkView()
	{
		if(viewList == null)
		{
			viewList = new ArrayList<View>();
		}
		final View remark = layoutInflater.inflate(R.layout.remark_item, null);
		ImageButton delete = (ImageButton)remark.findViewById(R.id.remark_delete);
		TextEditView name = (TextEditView)remark.findViewById(R.id.derive_name);
		TextEditView type = (TextEditView)remark.findViewById(R.id.derive_type);
		TextEditView compnay = (TextEditView)remark.findViewById(R.id.compnay_num);
		name.setTitle("设备名称");
		type.setTitle("型号");
		compnay.setTitle("公司编码");
		delete.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				remarks.removeView(remark);
				viewList.remove(remark);
			}
		});
		remarks.addView(remark);
		viewList.add(remark);
		return remark;
	}
	
	/**
	 * 初始化组件和数据
	 * @param view
	 */
	private void initView(View view) {
		addRemark = (ImageButton)view.findViewById(R.id.remark_add);
		addRemark.setOnClickListener(new OnClickListener() 
		{
			@Override
			public void onClick(View v) {
				addRemarkView();
			}
		});
		remarks = (LinearLayout)view.findViewById(R.id.remarks_layout);
		viewList = new ArrayList<View>();
		addRemarkView();
		data = ((TaskActivityGroup)getActivity()).getDataFragment4();
	}
	
	@Override
	public void onResume() {
		initViewData();
		super.onResume();
	}
	
	/**
	 * 获得数据
	 * @return
	 */
	public HashMap<String, String> getData() 
	{
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		if(viewList != null && viewList.size() > 0)
		{
			String remarkText = "";
			for (int i = 0; i < viewList.size(); i++) 
			{
				View remark = viewList.get(i);
				TextEditView name = (TextEditView)remark.findViewById(R.id.derive_name);
				TextEditView type = (TextEditView)remark.findViewById(R.id.derive_type);
				TextEditView compnay = (TextEditView)remark.findViewById(R.id.compnay_num);
				String nameText = name.getEditValue();
				String typeText = type.getEditValue();
				String compnayText = compnay.getEditValue();
				if((nameText == null || nameText.length() < 1)
						&& (typeText == null || typeText.length() < 1)
						&&(compnayText == null || compnayText.length() < 1))
				{
					continue;
				}
				if(i < viewList.size() - 1)
				{
					remarkText += nameText + "#";
					remarkText += typeText + "#";
					remarkText += compnayText + "#;";
				}
				else
				{
					remarkText += nameText + "#";
					remarkText += typeText + "#";
					remarkText += compnayText + "#";
				}
			}
			data.put(DataBaseUtil.task_detail_type2_table_clos[36], remarkText);
		}
		return data;
	}

	/**
	 * 设置页面数据
	 * @param data
	 */
	public void initViewData() {
		if(data != null)
		{
			String remarkText = data.get(DataBaseUtil.task_detail_type2_table_clos[36]);
			if(remarkText != null && remarkText.length() > 0 && remarkText.contains("#"))
			{
				String[] remarkArray = remarkText.split(";");
				for (int i = 0; i < remarkArray.length; i++) 
				{
					String temp=remarkArray[i];
					String[] obj=temp.split("#");
					if(i == 0)
					{
						View remark= viewList.get(i);
						TextEditView name = (TextEditView)remark.findViewById(R.id.derive_name);
						TextEditView type = (TextEditView)remark.findViewById(R.id.derive_type);
						TextEditView compnay = (TextEditView)remark.findViewById(R.id.compnay_num);
						
						if(obj!=null&&obj.length>0)
						{
							name.setValue((obj)[0]);
							if(obj.length>1)
							{
								type.setValue((obj)[1]);
							}
							if(obj.length>2)
							{
								compnay.setValue((obj)[2]);
							}
						}
					}
					else if(temp != null && temp.length() > 0)
					{
						View remark = addRemarkView();
						TextEditView name = (TextEditView)remark.findViewById(R.id.derive_name);
						TextEditView type = (TextEditView)remark.findViewById(R.id.derive_type);
						TextEditView compnay = (TextEditView)remark.findViewById(R.id.compnay_num);
						
						if(obj!=null&&obj.length>0)
						{
							name.setValue((obj)[0]);
							if(obj.length>1)
							{
								type.setValue((obj)[1]);
							}
							if(obj.length>2)
							{
								compnay.setValue((obj)[2]);
							}
						}
					}
				}
			}
		}
	}
}
